﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Plugin.Core;
using Microsoft.Win32;

namespace Plugin.Configs
{
    /// <summary>
    /// this class can be used for save/load configorations by 
    /// an XML file
    /// </summary>
    public class XMLConfig : Dictionary<string, object>, IConfigoration
    {

        public void Save(params object[] Parameters)
        {
            DataTable dt = new DataTable("Configoration");
            dt.Columns.Add("Key", typeof(string));
            dt.Columns.Add("Value", typeof(object));
            string[] keys = Keys.ToArray<string>();
            object[] values = Values.ToArray<object>();
            for (int i = 0; i < Count; i++)
            {
                dt.Rows.Add(keys[i], values[i]);
            }
            dt.WriteXml(Parameters[0].ToString(), true);

        }

        public void Load(params object[] Parameters)
        {
            DataTable dt = new DataTable("Configoration");
            dt.Columns.Add("Key", typeof(string));
            dt.Columns.Add("Value", typeof(object));
            dt.ReadXml(Parameters[0].ToString());
            Clear();
            foreach (DataRow row in dt.Rows)
            {
                this[row[0].ToString()] = row[1];
            }

        }

        public bool Exist(string Key)
        {
            return this.Keys.ToList<string>().Exists(new Predicate<string>(delegate(string s) { return s == Key; }));
        }

    }
    /// <summary>
    /// this class can be used for save/load configorations by
    /// win registry
    /// </summary>
    public class RegistryConfig : Dictionary<string, object>, IConfigoration
    {

        public void Save(params object[] Parameters)
        {
            RegistryKey main = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
            main = main.OpenSubKey("Zagros", true);
            if (main == null)
                main = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).CreateSubKey("Armani");
            string[] keys = Keys.ToArray<string>();
            object[] values = Values.ToArray<object>();
            for (int i = 0; i < Count; i++)
            {
                main.SetValue(keys[i], values[i]);
            }
            main.Close();
        }

        public void Load(params object[] Parameters)
        {
            RegistryKey main = Registry.LocalMachine.OpenSubKey("SOFTWARE", false);
            main = main.OpenSubKey("Zagros");
            if (main == null) return;
            string[] keys = main.GetValueNames();
            foreach (string key in keys)
            {
                this[key] = main.GetValue(key);
            }
            main.Close();
        }

        public bool Exist(string Key)
        {
            return this.Keys.ToList<string>().Exists(new Predicate<string>(delegate(string s) { return s == Key; }));
        }

    }
}
